/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.batavia.impl;

import com.batavia.inter.LoginInterface;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.util.Cookie;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.joda.time.DateTime;

/**
 *
 * @author Saiful Bahri
 * blog    : http://bahrie27.wordpress.com/
 * twitter : @bahrie127
 */
public class ImplementLogin implements LoginInterface {

    //okie cookie = new Cookie("web.batavia-air.com", "PHPSESSID", "3e1c875860c93f83036a9cea1ba4a074", "/", expire.toDate(), false);
    public Cookie login(String username, String password) {
        try {
            WebClient client = new WebClient(BrowserVersion.getDefault());

            client.setThrowExceptionOnScriptError(false);
            client.setCssEnabled(false);
            final HtmlPage page = client.getPage("http://web.batavia-air.com/MyPage/login.php");
            HtmlForm form = page.getFormByName("loginForm");
            form.getInputByName("useridmp").setValueAttribute(username);
            form.getInputByName("passwordmp").setValueAttribute(password);

            HtmlSubmitInput button = form.getInputByValue("sign in!");

            final HtmlPage page2 = button.click();
            try {
                HtmlForm form2 = page2.getFormByName("loginForm");

                return null;
            } catch (ElementNotFoundException enfe) {
                return client.getCookieManager().getCookie("PHPSESSID");
            }
            
        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return null;
        } catch (IOException e) {

            e.printStackTrace();  ///To change body of catch statement use File | Settings | File Templates.
            return null;
        }
    }

    public boolean cekLogin(Cookie cookie) {
        try {
            WebClient client = new WebClient(BrowserVersion.getDefault());
            client.setThrowExceptionOnScriptError(false);
            client.setCssEnabled(false);
            client.getCookieManager().addCookie(cookie);
            final HtmlPage page = client.getPage("http://web.batavia-air.com/MyPage/login.php");
            try {
                HtmlForm form = page.getFormByName("loginForm");

                return false;
            } catch (ElementNotFoundException enfe) {
                return true;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        } catch (FailingHttpStatusCodeException ex) {
            ex.printStackTrace();
            return false;
        }

    }
}
